﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BaBaoFan.Modifier.Data;
using BaBaoFan.Modifier.Model;
using Dapper;
using System.Linq;
using AutoMapper;

namespace BaBaoFan.Modifier.Data
{
    public class MillInfoRepository : IMillInfoRepository
    {
        private readonly IDbConnection _conn;
        public MillInfoRepository(DbConnection conn)
        {
            _conn = conn.Connection;
        }

        public bool Add(MillInfo millInfo)
        {
            string sql = "insert into millinfo (name,mill_price,mill_kind) values (@Name,@MillPrice,@MillKind)";
            return _conn.Execute(sql, millInfo) > 0;
        }

        public bool Delete(int[] ids)
        {
            string sql = "update millinfo set isdel=1 where id in @ids";
            return _conn.Execute(sql, new { ids }) > 0;
        }

        public MillInfo GetMillById(int id)
        {
            string sql = "select id as Id,name as Name,mill_price as MillPrice,mill_kind as MillKind from millinfo where id=@id";
            return _conn.QuerySingle<MillInfo>(sql, new { id });
        }

        public IEnumerable<MillInfoDto> GetMillInfos()
        {
            string sql = "select id as Id,name as Name,mill_price as MillPrice,mill_kind as MillKind from millinfo where isdel=0";
            var list = _conn.Query<MillInfo>(sql);
            return list.Select(x => Mapper.Map<MillInfo, MillInfoDto>(x));
        }

        public bool Update(MillInfo millInfo)
        {
            string sql = "update millinfo set name=@Name, mill_price=@MillPrice,mill_kind=@MillKind where id=@Id";
            return _conn.Execute(sql, millInfo) > 0;
        }
    }
}
